// utils/canvasOptimize.js
export const enableCanvasOptimization = () => {
  // 只在浏览器环境且支持willReadFrequently时启用
  if (typeof window === 'undefined' || 
      typeof HTMLCanvasElement === 'undefined' ||
      !window.navigator || 
      !window.navigator.userAgent) {
    return 'undefined';
  }
  
  // 检查用户代理，确保在支持的浏览器中运行
  const ua = window.navigator.userAgent;
  const isChrome = ua.includes('Chrome') && !ua.includes('Edg');
  const isFirefox = ua.includes('Firefox');
  
  if (isChrome || isFirefox) {
    try {
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d', { willReadFrequently: true });
      
      if (ctx && 'willReadFrequently' in ctx) {
        const originalGetContext = HTMLCanvasElement.prototype.getContext;
        
        HTMLCanvasElement.prototype.getContext = function(type, options) {
          if (type === '2d') {
            options = options || {};
            options.willReadFrequently = true;
          }
          return originalGetContext.call(this, type, options);
        };
        
        return true;
      }
    } catch (e) {
      console.warn('Canvas优化失败:', e);
    }
  }
  
  return false;
};

